using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// Scene扩展功能
/// </summary>
public static class SceneExtension
{
    /// <summary>
    /// 在场景中查找指定名字的物体(包括未激活)
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="name"></param>
    /// <returns>返回查找到的物体，没找到返回null</returns>
    public static GameObject ExFindGameObject(this Scene scene, string name)
    {
        List<GameObject> list = new List<GameObject>(scene.rootCount);
        scene.GetRootGameObjects(list);
        foreach (GameObject gameObject in list)
        {
            foreach (Transform t in gameObject.GetComponentsInChildren<Transform>(true))
            {
                if (t.name == name)
                {
                    return t.gameObject;
                }
            }
        }
        return null;
    }
    /// <summary>
    /// 在场景中查找所有符合指定名字的物体(包括未激活)
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="name"></param>
    /// <returns>返回所有查找到的物体</returns>
    public static GameObject[] ExFindGameObjects(this Scene scene, string name)
    {
        List<GameObject> list = new List<GameObject>(scene.rootCount);
        List<GameObject> findList = new List<GameObject>();
        scene.GetRootGameObjects(list);
        foreach (GameObject gameObject in list)
        {
            foreach (Transform t in gameObject.GetComponentsInChildren<Transform>(true))
            {
                if (t.name == name)
                {
                    findList.Add(t.gameObject);
                }
            }
        }
        return findList.ToArray();
    }
}